//
//  UITextView+BPUIKit.m
//  BPUIKit_Example
//
//  Created by 404 on 2020/7/30.
//  Copyright © 2020 404. All rights reserved.
//

#import "UITextView+BPUIKit.h"

@implementation UITextView (BPUIKit)

- (void)contentSizeToFit{
    if([self.text length] > 0){
       
        CGSize contentSize = self.contentSize;
        UIEdgeInsets offset;
        CGSize newSize = contentSize;
        
        //如果文字内容高度没有超过textView的高度
        if(contentSize.height <= self.frame.size.height){
            //textView的高度减去文字高度除以2就是Y方向的偏移量，也就是textView的上内边距
            CGFloat offsetY = (self.frame.size.height - contentSize.height)/2;
            offset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
        }else{
            //如果文字高度超出textView的高度
            newSize = self.frame.size;
            offset = UIEdgeInsetsZero;
           //通过一个while循环，设置textView的文字大小，使内容不超过整个textView的高度（这个根据需要可以自己设置）
            while (contentSize.height > self.frame.size.height)
            {
                [self setFont:self.font];
                contentSize = self.contentSize;
            }
            newSize = contentSize;
        }
        
        //根据前面计算设置textView的ContentSize和Y方向偏移量
        [self setContentSize:newSize];
        [self setContentInset:offset];
        
    }
}

- (void)setLongPressGestureEnable:(BOOL)longPressGestureEnable{
    for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
      if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
          recognizer.enabled = longPressGestureEnable;
          break;
      }
    }
}

@end
